package com.company;

import sun.misc.BASE64Decoder;

import java.io.*;

/**
 * base64转图片
 */
public class Base64ToBitmapUtils {

    public static void base64ToImage(String sBase65Path, String dImagePath) {


        try {
            InputStream is = new FileInputStream(sBase65Path);
            byte[] sBytes = new byte[is.available()];
            is.read(sBytes);
            is.close();

            OutputStream out = new FileOutputStream(dImagePath);
            // Base64解码
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] b = decoder.decodeBuffer(new String(sBytes));
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            out.write(b);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Base64ToBitmapUtils.base64ToImage("E:\\base64.txt", "E:\\img.png");
    }
}
